package com.opgea.tutor.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opgea.tutor.service.StudentService;
import com.opgea.tutor.util.FacesUtil;
import com.opgea.tutor.util.PageConstants;
import com.opgea.tutor.web.dto.AddressDTO;
import com.opgea.tutor.web.dto.EmailDTO;
import com.opgea.tutor.web.dto.LocationDTO;
import com.opgea.tutor.web.dto.PhoneDTO;
import com.opgea.tutor.web.dto.SpecializationDTO;
import com.opgea.tutor.web.dto.StudentDTO;

@ManagedBean(name = "studentController")
@RequestScoped
@Component
@Scope(value = "request")
public class StudentController {

    private Logger logger = Logger.getLogger(TutorController.class);

    @Autowired
    private StudentService studentService;

    private StudentDTO studentDTO = new StudentDTO();
    private AddressDTO addressDTO = new AddressDTO();
    private PhoneDTO phoneDTO = new PhoneDTO();
    private EmailDTO emailDTO = new EmailDTO();
    private SpecializationDTO specializationDTO = new SpecializationDTO();
    private LocationDTO locationDTO = new LocationDTO();

    private List<AddressDTO> addressDTOList = new ArrayList<AddressDTO>();
    private List<PhoneDTO> phoneDTOList = new ArrayList<PhoneDTO>();
    private List<EmailDTO> emailDTOList = new ArrayList<EmailDTO>();
    private List<SpecializationDTO> specializationDTOList = new ArrayList<SpecializationDTO>();

    @PostConstruct
    public void init() {
	if (FacesUtil.getHttpSession() != null
		&& FacesUtil.getSessionUserDataObject().getLoginIdPrimaryKey() != null) {
	    long studentId = FacesUtil.getSessionUserDataObject()
		    .getPrimaryKey();

	    phoneDTOList = studentService.findPhoneNumbers(studentId);
	    emailDTOList = studentService.findEmailIds(studentId);
	    if (!FacesUtil.getPageName().equalsIgnoreCase(
		    PageConstants.NEW_PAGE)) {
		studentDTO = studentService.findByLoginId(studentId);
		addressDTO = studentService.findAddress(studentId);
		if (phoneDTOList != null && phoneDTOList.size() > 0) {
		    phoneDTO = phoneDTOList.get(0);
		}
		if (emailDTOList != null && emailDTOList.size() > 0) {
		    emailDTO = emailDTOList.get(0);
		}
		if (specializationDTOList != null
			&& specializationDTOList.size() > 0) {
		    specializationDTO = specializationDTOList.get(0);
		}
	    }
	}
    }

    public StudentDTO getStudentDTO() {
	return studentDTO;
    }

    public void setStudentDTO(StudentDTO studentDTO) {
	this.studentDTO = studentDTO;
    }

    public AddressDTO getAddressDTO() {
	return addressDTO;
    }

    public void setAddressDTO(AddressDTO addressDTO) {
	this.addressDTO = addressDTO;
    }

    public List<AddressDTO> getAddressDTOList() {
	return addressDTOList;
    }

    public void setAddressDTOList(List<AddressDTO> addressDTOList) {
	this.addressDTOList = addressDTOList;
    }

    public PhoneDTO getPhoneDTO() {
	return phoneDTO;
    }

    public void setPhoneDTO(PhoneDTO phoneDTO) {
	this.phoneDTO = phoneDTO;
    }

    public EmailDTO getEmailDTO() {
	return emailDTO;
    }

    public void setEmailDTO(EmailDTO emailDTO) {
	this.emailDTO = emailDTO;
    }

    public LocationDTO getLocationDTO() {
	return locationDTO;
    }

    public void setLocationDTO(LocationDTO locationDTO) {
	this.locationDTO = locationDTO;
    }

    public List<PhoneDTO> getPhoneDTOList() {
	return phoneDTOList;
    }

    public void setPhoneDTOList(List<PhoneDTO> phoneDTOList) {
	this.phoneDTOList = phoneDTOList;
    }

    public List<EmailDTO> getEmailDTOList() {
	return emailDTOList;
    }

    public void setEmailDTOList(List<EmailDTO> emailDTOList) {
	this.emailDTOList = emailDTOList;
    }

    public SpecializationDTO getSpecializationDTO() {
	return specializationDTO;
    }

    public void setSpecializationDTO(SpecializationDTO specializationDTO) {
	this.specializationDTO = specializationDTO;
    }

    public List<SpecializationDTO> getSpecializationDTOList() {
	return specializationDTOList;
    }

    public void setSpecializationDTOList(
	    List<SpecializationDTO> specializationDTOList) {
	this.specializationDTOList = specializationDTOList;
    }

    public String updatePersonalDetails() {
	logger.info("updatePersonalDetails >> " + studentDTO);
	Long loginIdPrimaryKey = FacesUtil.getLoginDTOFromSession().getId();
	long studentId = FacesUtil.getSessionUserDataObject().getPrimaryKey();
	studentDTO.setId(studentId);
	studentDTO.setLoginIdPrimaryKey(loginIdPrimaryKey);
	studentService.update(studentDTO);
	FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		studentDTO.getFirstName()
			+ "\'s personal details updated successfully!", null);
	return "create.xhtml";
    }

    public String updateAddress() {
	logger.info("updateAddress >> " + addressDTO);
	Long studentId = FacesUtil.getSessionUserDataObject().getPrimaryKey();
	studentService.updateAddress(studentId, addressDTO);
	FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		"Address updated successfully!", null);
	return "create.xhtml";
    }

    public String updatePhone() {
	logger.info("updatePhone >> " + phoneDTO);
	Long studentId = FacesUtil.getSessionUserDataObject().getPrimaryKey();
	studentService.updatePhone(studentId, phoneDTO);
	phoneDTOList = studentService.findPhoneNumbers(studentId);
	FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		"Phone updated successfully!", null);
	return "create.xhtml";
    }

    public String updateEmail() {
	logger.info("updateEmail >> " + emailDTO);
	Long studentId = FacesUtil.getSessionUserDataObject().getPrimaryKey();
	studentService.updateEmail(studentId, emailDTO);
	emailDTOList = studentService.findEmailIds(studentId);
	FacesUtil.addMessage("messages", FacesMessage.SEVERITY_INFO,
		"Email updated successfully!", null);
	return "create.xhtml";
    }
}
